module func_arr_mod; 
use numeric_func;
implicit none;
logical, save :: FUNC_ARR_MOD_TRACE_ENTRY = .TRUE.;

type FUNC_ARR_T
  type(FUNC_VEC_PT), allocatable :: fa(:); !function-vector-pointer-array
end type

contains 

! Note: xp is shared
function new_func_arr(xp, n) result(res);  
  implicit none;  
  integer, intent(IN) :: n;
  type(FUNC_ARR_T), pointer :: res;  
  real(R14), pointer, intent(IN) :: xp(:);
  integer :: status, i, maxi, mini;
  call dbg_info("-->new_func_arr()", FUNC_ARR_MOD_TRACE_ENTRY);
  allocate(res, STAT=status);
  allocate(res%fa(n), STAT=status);
  mini = LBOUND(res%fa, 1);
  maxi = UBOUND(res%fa, 1);
  do i = mini, maxi
    res%fa(i)%fp => new_func(xp);
  end do
  return;
end function

subroutine del_func_arr_y(res);  implicit none;  
  type(FUNC_ARR_T), pointer :: res;
  integer :: i, maxi, mini;      
  call dbg_info("-->del_func_arr()", FUNC_ARR_MOD_TRACE_ENTRY);
  if (.NOT. ASSOCIATED(res)) return;
  mini = LBOUND(res%fa, 1);
  maxi = UBOUND(res%fa, 1);
  do i = mini, maxi
    call del_func_y(res%fa(i)%fp);
  end do
  DEALLOCATE(res%fa);    
  DEALLOCATE(res);     
  return;
end subroutine

end module 